package com.lagou.task.test04;

import java.util.*;

public class Test04 {

    public static void main(String[] args) {

        Test04 test04 = new Test04();
        test04.client.displayOperationInterface();

        Scanner sc = new Scanner(System.in);
        int operationNum;
        int modifyNum;
        while (true) {

            operationNum = sc.nextInt();

            //退出系统
            if (6 == operationNum) {
                System.out.println("已成功退出系统");
                break;
            }

            //增加用户
            if (1 == operationNum) {
                System.out.println("请输入用户信息:");
                String studentInfo = sc.next();
                String[] studentArray = studentInfo.split(",");
                Map<String, String> stu = new HashMap<>();
                for (String tmp : studentArray) {
                    String[] info = tmp.split("=");
                    stu.put(info[0], info[1]);
                }
                Student student = new Student(
                        Integer.parseInt(stu.get("id")),
                        stu.get("name"),
                        Integer.parseInt(stu.get("age"))
                );
                test04.studentManagement.add(student);
                System.out.println("-----------------");
                test04.client.displayOperationInterface();
                continue;
            }

            //删除用户
            if (2 == operationNum) {
                System.out.println("请输入用户的id:");
                int id = sc.nextInt();
                boolean isSuccess = test04.studentManagement.del(id);
                if (isSuccess) {
                    System.out.println("删除用户成功");
                } else {
                    System.out.println("id输入错误, 请重新输入");
                }
                System.out.println("-----------------");
                test04.client.displayOperationInterface();
                continue;
            }

            //增加用户
            if (5 == operationNum) {
                test04.studentManagement.show();
                System.out.println("-----------------");
                test04.client.displayOperationInterface();
            }



            //修改用户
            if (3 == operationNum) {
                System.out.println("请输入要修改的用户的id:");
                int id = sc.nextInt();
                Student student = test04.studentManagement.find(id);

                if (student == null) {
                    System.out.println("id输入错误, 请重新输入");
                    continue;
                } else {
                    test04.client.displayModifyInfo();
                    modifyNum = sc.nextInt();
                    if (1 == modifyNum) {
                        System.out.println("请输入要修改的姓名:");
                        String modifyName = sc.next();
                        student.setName(modifyName);
                    }

                    if (2 == modifyNum) {
                        System.out.println("请输入要修改的年龄:");
                        int modifyAge = sc.nextInt();
                        student.setAge(modifyAge);
                    }
                }

                System.out.println("-----------------");
                test04.client.displayOperationInterface();
                continue;
            }

            //查找用户
            if (4 == operationNum) {
                test04.client.displayFindInfo();
                int findId = sc.nextInt();

                if (1 == findId) {
                    System.out.println("请输入用户的id");
                    int userId = sc.nextInt();
                    Student student = test04.studentManagement.find(userId);
                    System.out.println("查找用户信息如下:");
                    System.out.println(student);
                }

                if (2 == findId) {
                    System.out.println("请输入用户的姓名:");
                    String userName = sc.next();
                    List<Student> students = test04.studentManagement.findByName(userName);
                    System.out.println("查找用户信息如下:");
                    System.out.println(students);
                }

                if (3 == findId) {
                    System.out.println("请输入用户的年龄:");
                    int userAge = sc.nextInt();
                    test04.studentManagement.findByAge(userAge);
                    List<Student> students = test04.studentManagement.findByAge(userAge);
                    System.out.println("查找用户信息如下:");
                    System.out.println(students);
                }

                System.out.println("-----------------");
                test04.client.displayOperationInterface();
                continue;
            }
        }
    }

    private StudentManagement studentManagement = new StudentManagement();

    private Client client = new Client();
}
